package com.laxcen.smoke.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceRequest;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.laxcen.smoke.config.CommonEnum;
import com.laxcen.smoke.dao.RemoteDao;
import com.laxcen.smoke.service.RemoteService;
import com.laxcen.smoke.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RemoteServiceImpl implements RemoteService {

    @Value("${accessKey}")
    private String accessKey;

    @Value("${accessSecret}")
    private String accessSecret;

    @Value("${productkey}")
    private String ProductKey;

    @Autowired
    private RemoteDao remoteDao;

    @Override
    public Response closeDoor(String smartno,String boxno,String orderno) {
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKey, accessSecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);

        InvokeThingServiceRequest request = new InvokeThingServiceRequest();
        request.setProductKey(ProductKey);
        request.setDeviceName(smartno);
//        remoteDao.getBoxno(boxno)
        String param = "{\"DeviceNo\":\""+smartno+"\",\"BoxNo\":\""+boxno+"\",\"OrderNo\":\""+orderno+"\"}";
        request.setArgs(param);
        request.setIdentifier("RemoteOpen");
//        request.setMessageContent(Base64.encodeBase64String(param.getBytes()));
//        request.setTopicFullName("/sys/a12bM7wcGVy/"+devicename+"/thing/event/RemoteOpDoor/post");
        if (remoteDao.getBoxno(boxno)==null){
            return new Response(CommonEnum.BOX_IS_NOT_EXIST);
        }
        if (remoteDao.getSmartsno(smartno)==null){
            return new Response(CommonEnum.SMART_IS_NOT_EXIST);
        }
//        request.setArgs("{}");
//        request.setMessageContent(Base64.encodeBase64String("1".getBytes()));
//        request.setTopicFullName("/sys/a12bM7wcGVy/Oufu001/thing/event/property/post_reply");
//        request.setQos(0); //目前支持QoS0和QoS1。
        try
        {
            InvokeThingServiceResponse response = client.getAcsResponse(request);
//            PubResponse response = client.getAcsResponse(request);
//            if (response.getSuccess()==false){
//                Response response1 = new Response();
//                response1.setMsg("远程开门失败");
//                response1.setData(response.getErrorMessage());
//                return response1;
//            }
//            System.out.println(response.getData().getResult());
            System.out.println(response.getSuccess());
            System.out.println(response.getErrorMessage());
            Response responses = new Response();
            responses.setOK();
            return responses;
        }
        catch (ServerException e)
        {
            e.printStackTrace();
        }
        catch (ClientException e)
        {
            e.printStackTrace();
        }
        Response response = new Response();
        response.setOK();
        return  response;
    }



}

